package com.lyc.task.scheduled;

import com.lyc.task.config.BookUrlConfig;
import com.lyc.task.service.QishuService;
import lombok.extern.slf4j.Slf4j;
import lombok.val;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

/**
 * Created by 廖师兄
 * 2017-05-26 00:49
 */
@Component
@Slf4j
public class QishuScheduled {

    @Autowired
    private QishuService qishuService;

    @Autowired
    private BookUrlConfig bookUrlConfig;

    /**
     * 拉取最新的内容(每小时跑一次)
     */
    @Scheduled(fixedDelay = 3600*1000)
    public void fetchLast() {
        log.info("【拉取最新的奇书网书籍】开始...");
        val outIdList = qishuService.fetchList();
        outIdList.stream()
                .forEach(e -> {
                    qishuService.setOutId(e);
                    qishuService.run();
                });
        log.info("【拉取最新的奇书网书籍】结束.");
    }

//    @Scheduled(fixedDelay = 3600*1000*100)
    public void fetchBook() throws Exception {
        log.info("开始循环拉取...");
        Integer i = bookUrlConfig.getStart();
        while (i < bookUrlConfig.getEnd()) {
            qishuService.setOutId(String.valueOf(i));
            try {
                qishuService.run();
            }catch (Exception e){
                log.error(e.getMessage());
            }
            i++;
        }
    }
}
